package com.proposition.paper.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.proposition.commons.model.ExamAcupointAdult;
import com.proposition.commons.model.ExamAnswer10;
import com.proposition.commons.model.ExamAnswer30;
import com.proposition.commons.model.PaperExam;
import com.proposition.exam.mapper.ExamAcupointAdultMapper;
import com.proposition.paper.mapper.PaperExamMapper;
import com.proposition.paper.service.PaperExportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 试题导出--客观化
 *
 * @author 杨凯
 * @create 2018-04-16 19:05
 **/
@Service("exportKeGuanHua")
public class PaperExportKeGuanHuaServiceImpl implements PaperExportService {


	@Autowired
	private PaperExamMapper examMapper;

	@Autowired
	private ExamAcupointAdultMapper adultMapper;

	@Override
	public Map<String, Object> getPaperAndAnswerById(String paperId) {
		Map<String,Object> map = new HashMap<>();
		ExamAnswer30 examAnswer30 = null;
		ExamAcupointAdult examAcupointAdult = null;
		List<ExamAcupointAdult> examAcupointAdultList = new ArrayList<ExamAcupointAdult>();
		ExamAnswer10 examAnswer10 = null;
		List<ExamAnswer10> answer10List = new ArrayList<ExamAnswer10>();
		PaperExam paperExam = new PaperExam();
		paperExam.setPaperId(paperId);
		//查询试卷对应试题
		paperExam = examMapper.selectOne(paperExam);
		String[] exam1 = paperExam.getExam1().split(",");
		String[] exam2 = paperExam.getExam2().split(",");
		String[] exam3 = paperExam.getExam3().split(",");
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < exam1.length; i++) {
			examAcupointAdult = adultMapper.selectExamAcupointAdultById(exam1[i]);
			examAcupointAdultList.add(examAcupointAdult);
			sb.append(examAcupointAdult.getStem())
					.append("、");
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append("。");
		map.put("exam1", sb.toString());
		map.put("exam1list", examAcupointAdultList);
		//清空StringBuilder
		sb.setLength(0);
		for (int i = 0; i < exam2.length; i++) {
			examAnswer10 = new ExamAnswer10().selectOne(new EntityWrapper().eq("id", exam2[i]));
			answer10List.add(examAnswer10);
			sb.append(i + 1)
					.append(".")
					.append(examAnswer10.getStem())
					.append("。")
					.append("（"+examAnswer10.getTotalScore()+"分）")
					.append("<w:br/>");
		}

		map.put("exam2list", answer10List);
		map.put("exam2", sb.substring(0, sb.length() - 7));
		examAnswer30 = new ExamAnswer30().selectOne(new EntityWrapper().eq("id", exam3[0]));
		map.put("exam3", examAnswer30);
		return map;
	}
}
